package cn.iocoder.yudao.module.wms.controller.admin.vendorclassify;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.wms.controller.admin.vendorclassify.vo.VendorClassifyPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.vendorclassify.vo.VendorClassifyRespVO;
import cn.iocoder.yudao.module.wms.controller.admin.vendorclassify.vo.VendorClassifySaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.vendorclassify.VendorClassifyDO;
import cn.iocoder.yudao.module.wms.service.vendorclassify.VendorClassifyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 供应商分类")
@RestController
@RequestMapping("/wms/vendor-classify")
@Validated
public class VendorClassifyController {

    @Resource
    private VendorClassifyService vendorClassifyService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商分类")
    @PreAuthorize("@ss.hasPermission('wms:vendor-classify:create')")
    public CommonResult<Long> createVendorClassify(@Valid @RequestBody VendorClassifySaveReqVO createReqVO) {
        return success(vendorClassifyService.createVendorClassify(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商分类")
    @PreAuthorize("@ss.hasPermission('wms:vendor-classify:update')")
    public CommonResult<Boolean> updateVendorClassify(@Valid @RequestBody VendorClassifySaveReqVO updateReqVO) {
        vendorClassifyService.updateVendorClassify(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:vendor-classify:delete')")
    public CommonResult<Boolean> deleteVendorClassify(@RequestParam("id") Long id) {
        vendorClassifyService.deleteVendorClassify(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:vendor-classify:query')")
    public CommonResult<VendorClassifyRespVO> getVendorClassify(@RequestParam("id") Long id) {
        VendorClassifyDO vendorClassify = vendorClassifyService.getVendorClassify(id);
        return success(BeanUtils.toBean(vendorClassify, VendorClassifyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商分类列表")
    @PreAuthorize("@ss.hasPermission('wms:vendor-classify:query')")
    public CommonResult<PageResult<VendorClassifyRespVO>> getVendorClassifyPage(@Valid VendorClassifyPageReqVO pageReqVO) {
        PageResult<VendorClassifyDO> pageResult = vendorClassifyService.getVendorClassifyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VendorClassifyRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得存货分类精简列表", description = "主要用于前端的下拉选项和树状列表")
    @PreAuthorize("@ss.hasPermission('wms:inventory-classify:query')")
    public CommonResult<List<VendorClassifyRespVO>> getVendorClassifySimpleList() {
        List<VendorClassifyDO> list = vendorClassifyService.getVendorClassifyList();
        return success(convertList(list, classify -> new VendorClassifyRespVO()
                .setId(classify.getId()).setName(classify.getName()).setParentId(classify.getParentId())));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商分类 Excel")
    @PreAuthorize("@ss.hasPermission('wms:vendor-classify:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVendorClassifyExcel(@Valid VendorClassifyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VendorClassifyDO> list = vendorClassifyService.getVendorClassifyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商分类.xls", "数据", VendorClassifyRespVO.class,
                        BeanUtils.toBean(list, VendorClassifyRespVO.class));
    }

}